Code 
Wavelet analysis for localization

/* The implementation of an algorithm for calculating generalized spectral coefficients by wavelet analysis for localization using Kravwtchouk functions as the mother wavelets  */


char *m6[9]={"k4-4f","k8-4f","k16-4f","k32-4f","k64-4f","k128-4f","k256-4f","k512-4f","k1024-4f"};

int PreFFT();
 double M_2xPI;

union {
  char v[4*M];
  float p[M];
      }kr;
 

/*---open file "*.dat"-data file ------*/

_outtext(" name of data File?=           ");
   gets(jp);

if((f2=fopen(jp,"rt"))==0)
      printf("error-open file %s",jp);

for(j=0;j<1024;j++)
 fscanf(f2,"%d",&y[j]);

if((fclose(f2))!=0)
      {
      printf("error-close file %s",jp);
       exit(1);
       }

for(j1=0;j1<9;j1++){ /* start a cycle on windows of different scales  */

n=10-j1;
t1=pow(2.0,n); /* the length of the window */
m1=4*t1;
tm=4; /* number of Krawtchouk functions  */


   strcat(namef,m6[n-2]);
 
/* open file "kr?f" - opening a file of calculated in advance values of Fourier transformation of the Krawtchouk functions of the length of 2 to degree n  */

if((f1=open(namef,O_RDONLY|O_BINARY))==-1)

for(a0=0;a0<t/t1;a0++){  /* cycle by a0 - next window of signal shift  */

for(i=a0*t1;i<t1+a0*t1;i++){   /* signal preparation for Fourier transformation  */
	   A1[i].y=0.0;
	A1[i-a0*t1].x=yop[i];
	    	    }
k=PreFFT(W,n);
FFT(A1,W,n,k,+1);/*  Fourier transformation  of the signal */

for(l=1;l<K;l++){   /* cycle by p=l/K - asymmetry coefficient  */

for(j=0;j<tm;j++){  /* cycle on the number of Krawtchouk function  */

              /* read Fourier transformation of Krawtchouk functions  */
	       read(f1,kr.v,m1);                   
	     for(i=0;i<t1;i++)
             A2[i].x=kr.p[i];
                
	     read(f1,kr.v,m1);
             for(i=0;i<t1;i++)
	     A2[i].y=kr.p[i];


for(i=0;i<t1;i++){    /* multiplication of Fourier transforms of signal and Krawtchouck functions  */
A3[i].x=(A1[i].x*A2[i].x+A1[i].y*A2[i].y)*t1;
A3[i].y=(A1[i].x*A2[i].y-A2[i].x*A1[i].y)*t1;
}
FFT(A3,W,n,k,-1); /* inverse Fourier transformation */

for(b=0;b<t1;b++){  /*  shift in the analysis window  */
kof.c[b]=A3[b].x;  /*  spectral coefficients  */

tn=pow(kof.c[b],2.0);
f[b][l]=tn+f[b][l];  /* energy functional */

}
	 
             } /*  close cycle by j */

	     } /*  close cycle by l */

for(l=1;l<K;l++) /*  search of the maximum of the energy functional */
for(b=0;b<t1;b++)
 if(f[b][l]>mf)
      {
     mf=f[b][l];
     sh[a0][n-2]=b;
     we[a0][n-2]=l;
        };

i1=sh[a0][n-2];  /* shift */
i2=we[a0][n-2];  /* asymmetry of the signal */

if((l<10)&&(l>5))
i3=t1+(l*0.1+0.5)*t1;

if((l<=5)&&(l>0))
i3=t1+(l*0.1-0.5)*t1;

for(i=a0*t1;i<t1+a0*t1;i++){
tn=fmod(i3-i-1,t1);
i4=abs(tn);

fs[i][j1]=f[i4][i2]; /* energy functional in the window j1 with maximum corresponding to the signal extremum */
}

}


} /* end a cycle on windows of different scales */

typedef struct complex  cmplx;

/* preparation for the Fourier transformation */
int PreFFT(cmplx w[],int m)
{ double p;
  int i,n;
  double M_PI;
  M_PI=3.1415925;
  for (n=1,p=M_PI,i=0;i<m;i++,p/=2.,n*=2)
      { w[i].x= cos(p);
        w[i].y=-sin(p);
      }
  return(n);
}

/* Fourier transformation */
FFT(cmplx a[],cmplx w[],int m,int n,int jt)
{ char lbt,ch;
  cmplx u,v,t,g,h,CmpAr();
  int j,m2,nm1,k,le1,le,ip,nv2,i,l;
  lbt=jt<0;
  nv2=n/2; nm1=n-1; j=0;
  for ( j=0,i=0; i<nm1; i++,j+=k )
      { if (i<j) {  t=a[j]; a[j]=a[i]; a[i]=t; }
        k=nv2;
        while ( k<=j ) { j=j-k; k=k/2; }
       }
  for ( le1=1,l=0; l<m; l++,le1=le )
      { le=le1+le1; u.x=1.; u.y=0;
        for (j=0; j<le1; j++)
            { for (i=j; i<n; i+=le)
                  { ip=i+le1;
                    t=CmpAr(a[ip],'*',u);
                    a[ip]=CmpAr(a[i],'-',t);
                    a[i] =CmpAr(a[i],'+',t);
                   }
              v=w[l];
              if (lbt) v.y=-v.y;
              u=CmpAr(u,'*',v);
             }
      }
  if (!lbt) { v.x=n; v.y=0;
              for (i=0; i<n; i++) a[i]=CmpAr(a[i],'/',v);
             }
}

/*    Complex ariphmetics                  */
/*    '+','-','*','/','^'-the float power  */

cmplx CmpAr(a,d,b)
cmplx a;
cmplx b;
char  d;
{ cmplx c;
  double r,fi;
  switch ( d )
  { case '+': c.x=a.x+b.x;
              c.y=a.y+b.y;
              break;
    case '-': c.x=a.x-b.x;
              c.y=a.y-b.y;
              break;
    case '*': c.x=a.x*b.x-a.y*b.y;
              c.y=a.x*b.y+b.x*a.y;
              break;
    case '/': r=b.x*b.x+b.y*b.y;
              c.x=(a.x*b.x+a.y*b.y)/r;
              c.y=(a.y*b.x-a.x*b.y)/r;
              break;
    case '^': r=sqrt(a.x*a.x+a.y*a.y);
              fi=acos(a.x/r);
              r=pow(r,b.x);
              c.x=cos(fi*b.x)*r;
              c.y=sin(fi*b.x)*r;
              break;
  }
  return (c);
}


